<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="flex-row">
          <el-image src="/logo.png"></el-image>
          <span style="font-size: 24px;font-weight: bold;margin-left: 10px">校园工鸟系统</span>
        </div>
      </template>
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="账号">
            <template #prefix>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="密码" type="password">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" style="width: 100%" type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>

      <div style="text-align: center">
        <el-button link @click="router.push('/register')">立即注册</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {useStore} from 'vuex'
import {useRouter} from 'vue-router'
import {Lock, User} from '@element-plus/icons-vue'

const store = useStore()
const router = useRouter()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  username: '10081',
  password: '123456'
})

const rules = {
  username: [{required: true, message: '请输入账号', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}]
}

const handleLogin = async () => {
  try {
    loading.value = true
    await loginFormRef.value.validate()
    await store.dispatch('login', loginForm)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url("/back.png");
}

.login-card {
  width: 400px;
}
</style> 